package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {

    @Reference
    private ContractService contractService;

    /**
     * 1. 列表
     */
    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        // 构造条件
        ContractExample example = new ContractExample();
        // 构造条件-排序
        example.setOrderByClause("create_time desc");
        ContractExample.Criteria criteria = example.createCriteria();
        // 构造条件-根据公司id查询
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        /**
         * 细粒度权限控制
         * 用户级别 degree
         *   4-普通员工
         *   3-管理本部门
         *   2-管理所有下属部门和人员
         */
        User user = getUser();
        if (user.getDegree() == 4){
            // 普通用户登陆，只能查看自己创建的购销合同
            criteria.andCreateByEqualTo(user.getId());
        } else if (user.getDegree() == 3){
            // 管理本部门
            criteria.andCreateDeptEqualTo(user.getDeptId());
        } else if (user.getDegree() ==2){
            // 管理所有下属部门和人员
            System.out.println("user.getDeptId()================"+user.getDeptId());
            PageInfo<Contract> pageInfo =
                    contractService.selectByDeptId(user.getDeptId(),pageNum,pageSize);
            request.setAttribute("pageInfo",pageInfo);
            return "cargo/contract/contract-list";
        }

        //1.调用service查询购销合同列表
        PageInfo<Contract> pageInfo =
                contractService.findByPage(example, pageNum, pageSize);
        //2.保存数据
        request.setAttribute("pageInfo",pageInfo);
        //3. 返回
        return "cargo/contract/contract-list";
    }

    /**
     * 2. 进入添加页面
     *
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "cargo/contract/contract-add";
    }

    /**
     * 3. 添加/修改
     */
    @RequestMapping("/edit")
    public String edit(Contract contract) {
        // 获取登录用户的所属企业信息（先写死）
        contract.setCompanyId(getLoginCompanyId());
        contract.setCompanyName(getLoginCompanyName());
        // 根据id判断
        if (StringUtils.isEmpty(contract.getId())) {
            // 添加创建者信息
            contract.setCreateBy(getUser().getId());
            contract.setCreateDept(getUser().getDeptId());
            // 添加
            contractService.save(contract);
        } else {
            // 修改
            contractService.update(contract);
        }

        return "redirect:/cargo/contract/list.do";
    }

    /**
     * 4. 进入修改页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        // 根据部门id查询
        Contract contract = contractService.findById(id);
        request.setAttribute("contract", contract);

        return "cargo/contract/contract-update";
    }

    /**
     * 5. 删除
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        contractService.delete(id);
        return "redirect:/cargo/contract/list.do";
    }

    /**
     * 购销合同（1）查看
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        //根据id进行查询
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        //跳转到修改界面
        return "cargo/contract/contract-view";
    }

    /**
     * 购销合同（2）提交，将状态由0改为1
     */
    @RequestMapping("/submit")
    public String submit(String id) {
        //1.构造购销合同对象
        Contract contract = new Contract();
        //2.设置id
        contract.setId(id);
        //3.设置状态
        contract.setState(1);
        //4.更新
        contractService.update(contract);
        //跳转到修改界面
        return "redirect:/cargo/contract/list.do";
    }

    /**
     * 购销合同（3）取消，将状态由1改为0
     */
    @RequestMapping("/cancel")
    public String cancel(String id) {
        //判断
        //1.构造购销合同对象
        Contract contract = new Contract();
        //2.设置id
        contract.setId(id);
        //3.设置状态
        contract.setState(0);
        //4.更新
        contractService.update(contract);
        //跳转到修改界面
        return "redirect:/cargo/contract/list.do";
    }
}